package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

/**
 * 安全密码设置
 *
 * @author sunmi on 2017/2/16.
 */
public class SecurityPasswordSettingsOperation {

    private static SecurityPasswordSettingsOperation instance;

    private SecurityPasswordSettings settings;

    private SecurityPasswordSettingsOperation() {
        settings = getSecurityPasswordSettings();
        if (settings == null) {
            settings = new SecurityPasswordSettings();
            saveSecurityPasswordSettings(settings);
        }
    }

    public static SecurityPasswordSettingsOperation getInstance() {
        if (instance == null) {
            instance = new SecurityPasswordSettingsOperation();
        }
        return instance;
    }

    public void saveSecurityPassword(String securityPassword) {
        SecurityPasswordSettings security = getSecurityPasswordSettings();
        security.setSecurityPassword(securityPassword);
        saveSecurityPasswordSettings(security);
    }

    public String getSecurityPassword() {
        SecurityPasswordSettings security = getSecurityPasswordSettings();
        return security.getSecurityPassword();
    }

    public void saveSecurityPasswordSettings(SecurityPasswordSettings securityPasswordSettings) {
        this.settings = securityPasswordSettings;
        SharedPreferencesUtil.saveObj(securityPasswordSettings, SecurityPasswordSettings.SECURITY_PASSWORD_CACHE_KEY);
    }

    public SecurityPasswordSettings getSecurityPasswordSettings() {
        if (settings == null) {
            Object obj = SharedPreferencesUtil.readObj(SecurityPasswordSettings.SECURITY_PASSWORD_CACHE_KEY);
            if (obj instanceof SecurityPasswordSettings) {
                settings = (SecurityPasswordSettings) obj;
            } else {
                settings = new SecurityPasswordSettings();
                saveSecurityPasswordSettings(settings);
            }
        }
        return settings;
    }

}
